package com.connector.client;

import com.connector.client.factory.ConnectorFactory;
import com.connector.client.mvp.AppActivityMapper;
import com.connector.client.mvp.AppPlaceHistoryMapper;
import com.connector.client.place.MainPlace;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class Connector implements EntryPoint {
	private Place defaultPlace = new MainPlace("main");
	private SimplePanel appWidget = new SimplePanel();
	
	@UiField
	private ConnectorMainPanel connectorMainPanel = new ConnectorMainPanel();

	public void onModuleLoad() {
		// Create ClientFactory using deferred binding so we can replace with
		// different
		// impls in gwt.xml
		ConnectorFactory connectorFactory = GWT.create(ConnectorFactory.class);
		EventBus eventBus = connectorFactory.getEventBus();
		PlaceController placeController = connectorFactory.getPlaceController();

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(connectorFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper,
				eventBus);
		activityManager.setDisplay(null);

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT
				.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
				historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		RootPanel.get().add(connectorMainPanel);
		// Goes to place represented on URL or default place
		historyHandler.handleCurrentHistory();

		GWT.log("User agent: " + Window.Navigator.getUserAgent());
	}
}
